package com.apikey.auth.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.Table;
import java.time.LocalDateTime;

/**
 * 操作历史实体类
 * 记录API Key的操作历史信息
 *
 * @author API Key Framework
 * @version 1.0
 */
@Entity
@Table(name = "operation_history", indexes = {
        @Index(name = "idx_api_key_id", columnList = "api_key_id"),
        @Index(name = "idx_operator_id", columnList = "operator_id"),
        @Index(name = "idx_operation_type", columnList = "operation_type"),
        @Index(name = "idx_created_at", columnList = "created_at")
})
public class OperationHistory {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "api_key_id", nullable = false)
    private Long apiKeyId;

    @Column(name = "operation_type", nullable = false, length = 50)
    private String operationType;

    @Column(name = "operator_id", nullable = false, length = 50)
    private String operatorId;

    @Column(name = "description", columnDefinition = "TEXT")
    private String description;

    @Column(name = "old_value", columnDefinition = "TEXT")
    private String oldValue;

    @Column(name = "new_value", columnDefinition = "TEXT")
    private String newValue;

    @Column(name = "ip_address", length = 45)
    private String ipAddress;

    @Column(name = "user_agent", columnDefinition = "TEXT")
    private String userAgent;

    @Column(name = "created_at", nullable = false)
    private LocalDateTime createdAt;

    /**
     * 默认构造函数
     */
    public OperationHistory() {
        this.createdAt = LocalDateTime.now();
    }

    /**
     * 构造函数
     *
     * @param apiKeyId      API Key ID
     * @param operationType 操作类型
     * @param operatorId    操作员ID
     * @param description   描述
     */
    public OperationHistory(Long apiKeyId, String operationType, String operatorId, String description) {
        this();
        this.apiKeyId = apiKeyId;
        this.operationType = operationType;
        this.operatorId = operatorId;
        this.description = description;
    }

    /**
     * 获取ID
     *
     * @return ID
     */
    public Long getId() {
        return id;
    }

    /**
     * 设置ID
     *
     * @param id ID
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * 获取API Key ID
     *
     * @return API Key ID
     */
    public Long getApiKeyId() {
        return apiKeyId;
    }

    /**
     * 设置API Key ID
     *
     * @param apiKeyId API Key ID
     */
    public void setApiKeyId(Long apiKeyId) {
        this.apiKeyId = apiKeyId;
    }

    /**
     * 获取操作类型
     *
     * @return 操作类型
     */
    public String getOperationType() {
        return operationType;
    }

    /**
     * 设置操作类型
     *
     * @param operationType 操作类型
     */
    public void setOperationType(String operationType) {
        this.operationType = operationType;
    }

    /**
     * 获取操作员ID
     *
     * @return 操作员ID
     */
    public String getOperatorId() {
        return operatorId;
    }

    /**
     * 设置操作员ID
     *
     * @param operatorId 操作员ID
     */
    public void setOperatorId(String operatorId) {
        this.operatorId = operatorId;
    }

    /**
     * 获取描述
     *
     * @return 描述
     */
    public String getDescription() {
        return description;
    }

    /**
     * 设置描述
     *
     * @param description 描述
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * 获取旧值
     *
     * @return 旧值
     */
    public String getOldValue() {
        return oldValue;
    }

    /**
     * 设置旧值
     *
     * @param oldValue 旧值
     */
    public void setOldValue(String oldValue) {
        this.oldValue = oldValue;
    }

    /**
     * 获取新值
     *
     * @return 新值
     */
    public String getNewValue() {
        return newValue;
    }

    /**
     * 设置新值
     *
     * @param newValue 新值
     */
    public void setNewValue(String newValue) {
        this.newValue = newValue;
    }

    /**
     * 获取IP地址
     *
     * @return IP地址
     */
    public String getIpAddress() {
        return ipAddress;
    }

    /**
     * 设置IP地址
     *
     * @param ipAddress IP地址
     */
    public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
    }

    /**
     * 获取用户代理
     *
     * @return 用户代理
     */
    public String getUserAgent() {
        return userAgent;
    }

    /**
     * 设置用户代理
     *
     * @param userAgent 用户代理
     */
    public void setUserAgent(String userAgent) {
        this.userAgent = userAgent;
    }

    /**
     * 获取创建时间
     *
     * @return 创建时间
     */
    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    /**
     * 设置创建时间
     *
     * @param createdAt 创建时间
     */
    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    @Override
    public String toString() {
        return "OperationHistory{" +
                "id=" + id +
                ", apiKeyId=" + apiKeyId +
                ", operationType='" + operationType + '\'' +
                ", operatorId='" + operatorId + '\'' +
                ", description='" + description + '\'' +
                ", createdAt=" + createdAt +
                '}';
    }
}